package com.waar.kickball.playerInput;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.waar.kickball.player.Player;

/**
 * @author Lance Burk
 *Copyright Lance + Craig 2013
 * 
 */
public class UserPositionInputReader {
	
	public UserPositionInputReader(){
		
	}
	
	/**
	 * Reads xml file for roster. yoloSwag
	 */
	public List<Player> readRoster(){
		File fXmlFile = new File(System.getProperty("user.dir") + "/conf/kickballLineup.xml");
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		List<Player> playerList = new ArrayList<Player>();
		try {
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document dom = db.parse(fXmlFile);
			dom.getDocumentElement().normalize();
			/**
			 * reads elements in roster tag.
			 */
			NodeList list = dom.getElementsByTagName("pitcher");
			for(int i = 0; i < list.getLength(); i++){
				Node node = list.item(i);
				if (node.getNodeType() == Node.ELEMENT_NODE) {
					Element element = (Element) node;
					String firstName = element.getElementsByTagName("firstname").item(0).getTextContent();
					String lastName = element.getElementsByTagName("lastname").item(0).getTextContent();
					String sex = element.getElementsByTagName("sex").item(0).getTextContent();
					playerList.add(new Player(firstName, lastName, sex));
				}
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		readPitcher();
		return playerList;
	}
	
	public void readPitcher() {
		File fXmlFile = new File(System.getProperty("user.dir") + "/conf/kickballLineup.xml");
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		List<Player> playerList = new ArrayList<Player>();
		try {
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document dom = db.parse(fXmlFile);
			dom.getDocumentElement().normalize();
			/**
			 * reads elements in roster tag.
			 */
			NodeList list = dom.getElementsByTagName("pitcher");
			for(int i = 0; i < list.getLength(); i++){
				Node node = list.item(i);
				if (node.getNodeType() == Node.ELEMENT_NODE) {
					Element element = (Element) node;
					String firstName = element.getElementsByTagName("first").item(0).getTextContent();
					String lastName = element.getElementsByTagName("second").item(0).getTextContent();
				}
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	
	
}
